﻿using SF.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.goods.orders
{
    public partial class order_list : SF.Web.UI.ManagePage
    {
        Model.manager mgr;
        string appWhere;
        protected string dis = "display:none;";
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;
        //&&1 待支付 0  支付完成
        protected int status;
        //&&1 在线支付 0 货到付款
        protected int payWay;
        //&&1 未打印 2 已打印 3 多次打印
        protected int express;
        //&&1 已处理 0  未处理
        protected int isDeal;
        protected string startTime = string.Empty;
        protected string endTime = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.status = DTRequest.GetQueryInt("status");
            this.payWay = DTRequest.GetQueryInt("payWay");
            this.express = DTRequest.GetQueryInt("express");
            this.isDeal = DTRequest.GetQueryInt("isDeal");
            this.startTime = DTRequest.GetQueryString("start_time");
            this.endTime = SF.Common.DTRequest.GetQueryString("end_time");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                if (Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager == null)
                {
                    Response.Redirect("/admin/login.aspx");
                }
                ChkAdminLevel("goods_orders_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
                appWhere = Utils.getWhereByInfo(mgr.busnieseNum.ToString(), Session[DTKeys.SESSION_APP_INFO].ToString(), Session[DTKeys.SESSION_TEMPLATES_INFO].ToString());
                if (string.IsNullOrEmpty(Session[DTKeys.SESSION_TEMPLATES_INFO].ToString()))
                {
                    JscriptMsg("请先在商品管理中选择模版！", "/admin/modeconfig/malltemplet_list.aspx");
                    return;
                }
                if (new BLL.sf_modeconfig().GetModel("settingNum='" + Session[DTKeys.SESSION_TEMPLATES_INFO].ToString() + "'").marketing_type == 1)
                {
                    dis = "display:block;";
                }

                RptBind(this.status, this.payWay, this.isDeal, this.express, appWhere+" and id>0 " + CombSqlTxt(this.startTime, this.endTime, this.keywords), " addTime desc");
            }
        }

        #region 数据绑定
        private void RptBind(int _status, int _payway, int _isDeal, int _express, string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            if (_status >= 0)
            {
                ddlStatus.SelectedValue = _status.ToString();
            }
            if (_payway >= 0)
            {
                ddlPayWay.SelectedValue = _payway.ToString();
            }
            if (_express >= 0)
            {
                ddlExpress.SelectedValue = _express.ToString();
            }
            if (_isDeal >= 0)
            {
                ddlDone.SelectedValue = _isDeal.ToString();
            }
            txtStartTime.Text = this.startTime;
            txtEndTime.Text = this.endTime;
            txtKeyWords.Text = this.keywords;
            if (_status != 0)
            {
                _strWhere = " isPay=" + _status + " and " + _strWhere;
            }
            if (_payway != 0)
            {
                _strWhere = " payWay=" + _payway + " and " + _strWhere;
            }
            if (_express != 0)
            {
                _strWhere = " isExpress=" + _express + " and " + _strWhere;
            }
            if (_isDeal != 0)
            {
                _strWhere = " isComDone=" + (_isDeal == 2 ? "0" : "1") + " and " + _strWhere;
            }


            BLL.sf_goods_order bll = new BLL.sf_goods_order();

            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            lblTotalMoney.Text = "0.0";
            System.Data.DataSet ds = bll.GetSumField("totalMoney", _strWhere);
            if (ds != null)
            {
                lblTotalMoney.Text = ds.Tables[0].Rows[0][0].ToString();
                if (string.IsNullOrEmpty(lblTotalMoney.Text))
                {
                    lblTotalMoney.Text = "0.0";
                }
            }
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("order_list.aspx", "start_time={0}&end_time={1}&status={2}&payWay={3}&express={4}&isDeal={5}&keywords={6}&page={7}", this.startTime, this.endTime, this.status.ToString(), this.payWay.ToString(), this.express.ToString(),
                _isDeal.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句
        protected string CombSqlTxt(string _startTime, string _endTime, string _keywords)
        {
            System.Text.StringBuilder strTemp = new System.Text.StringBuilder();
            _startTime = _startTime.Replace("'", "");
            _endTime = _endTime.Replace("'", "");
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(startTime))
            {
                strTemp.Append(" and addTime between '").Append(startTime).Append("'");
                if (!string.IsNullOrEmpty(endTime))
                    strTemp.Append(" and '").Append(endTime).Append("'");
                else
                    strTemp.Append(" and '").Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Append("'");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (orderNum like  '%")
                      .Append(_keywords)
                      .Append("%' or userNum like '%")
                      .Append(_keywords)
                      .Append("%' or addressText like '%")
                      .Append(_keywords)
                      .Append("') ");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回图文每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("order_list_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion


        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "start_time={0}&end_time={1}&status={2}&payWay={3}&express={4}&isDeal={5}&keywords={6}", Request.Form["txtStartTime"], Request.Form["txtEndTime"],
                this.ddlStatus.SelectedValue, this.ddlPayWay.SelectedValue,
                this.ddlExpress.SelectedValue, this.ddlDone.SelectedValue, txtKeyWords.Text));
        }

        //&&是否支付完成
        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "start_time={0}&end_time={1}&status={2}&payWay={3}&express={4}&isDeal={5}&keywords={6}", Request.Form["txtStartTime"], Request.Form["txtEndTime"],
                this.ddlStatus.SelectedValue, this.ddlPayWay.SelectedValue,
                this.ddlExpress.SelectedValue, this.ddlDone.SelectedValue, txtKeyWords.Text));
        }
        //&&打单信息
        protected void ddlExpress_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "start_time={0}&end_time={1}&status={2}&payWay={3}&express={4}&isDeal={5}&keywords={6}", Request.Form["txtStartTime"], Request.Form["txtEndTime"],
                this.ddlStatus.SelectedValue, this.ddlPayWay.SelectedValue,
                this.ddlExpress.SelectedValue, this.ddlDone.SelectedValue, txtKeyWords.Text));
        }
        //&&支付方式
        protected void ddlPayWay_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "start_time={0}&end_time={1}&status={2}&payWay={3}&express={4}&isDeal={5}&keywords={6}", Request.Form["txtStartTime"], Request.Form["txtEndTime"],
                this.ddlStatus.SelectedValue, this.ddlPayWay.SelectedValue,
                this.ddlExpress.SelectedValue, this.ddlDone.SelectedValue, txtKeyWords.Text));
        }
        //&&是否结算
        protected void ddlDone_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "start_time={0}&end_time={1}&status={2}&payWay={3}&express={4}&isDeal={5}&keywords={6}", Request.Form["txtStartTime"], Request.Form["txtEndTime"],
                this.ddlStatus.SelectedValue, this.ddlPayWay.SelectedValue,
                this.ddlExpress.SelectedValue, this.ddlDone.SelectedValue, txtKeyWords.Text));
        }
        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("order_list_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "start_time={0}&end_time={1}&status={2}&payWay={3}&express={4}&isDeal={5}&keywords={6}", txtStartTime.Text, txtEndTime.Text,
                this.ddlStatus.SelectedValue, this.ddlPayWay.SelectedValue, this.ddlExpress.SelectedValue,
                this.ddlDone.SelectedValue, txtKeyWords.Text));

        }

        ////下载EXCEL
        //protected void btndownExcel_Click(object sender, EventArgs e)
        //{
        //    //获取 配置文件 excel地址
        //    IDictionary dict = ConfigurationManager.GetSection("weixinPay") as IDictionary;
        //    string read_uri = dict["excel_uri"].ToString();
        //    string ip_uri = dict["ip_url"].ToString();
        //    //创建 excel对象
        //    Workbook wb = new Workbook(Utils.GetMapPath(@read_uri + @"/model/订单数据.xls"));
        //    Worksheet worksheet = wb.Worksheets[0];
        //    Cells cellsone = worksheet.Cells;
        //    string fileName = "订单数据" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
        //    //设置表头
        //    cellsone[1, 0].Value = fileName;
        //    cellsone[2, 7].Value = (Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager).real_name;
        //    cellsone[2, 9].Value = string.Format("{0:yyyy.MM.dd HH:mm:ss}", DateTime.Now);
        //    cellsone[3, 0].Value = "序号";
        //    cellsone[3, 1].Value = "订单编号";
        //    cellsone[3, 2].Value = "用户编号";
        //    cellsone[3, 3].Value = "支付方式";
        //    cellsone[3, 4].Value = "地址信息";
        //    cellsone[3, 5].Value = "小计";
        //    cellsone[3, 6].Value = "订单时间";
        //    cellsone[3, 7].Value = "支付时间";
        //    cellsone[3, 8].Value = "支付信息";
        //    cellsone[3, 9].Value = "物流信息";
        //    cellsone[3, 10].Value = "是否结算";
        //    //获取数据
        //    BLL.sf_goods_order bll = new BLL.sf_goods_order();
        //    string _strWhere = " id>0 " + CombSqlTxt(txtStartTime.Text, txtEndTime.Text, txtKeyWords.Text);
        //    if (status != 0)
        //    {
        //        _strWhere = " status=" + status + " and " + _strWhere;
        //    }
        //    if (payWay != 0)
        //    {
        //        _strWhere = " payWay=" + payWay + " and " + _strWhere;
        //    }
        //    if (express != 0)
        //    {
        //        _strWhere = " express=" + express + " and " + _strWhere;
        //    }
        //    if (isDeal != 0)
        //    {
        //        _strWhere = " isDone=" + (isDeal == 2 ? "0" : "1") + " and " + _strWhere;
        //    }
        //    DataTable dt = bll.GetList(10000, 1, _strWhere, " addTime desc", out this.totalCount).Tables[0];
        //    if (dt.Rows.Count > 0)
        //    {
        //        //添加数据
        //        for (int i = 4; i < dt.Rows.Count + 4; i++)
        //        {
        //            cellsone[i, 0].Value = (i - 3);
        //            cellsone[i, 1].Value = dt.Rows[i - 4]["orderNum"];
        //            cellsone[i, 2].Value = dt.Rows[i - 4]["userNum"];
        //            cellsone[i, 3].Value = dt.Rows[i - 4]["payWay"].ToString().Equals("1") ? "在线支付" : "货到付款";
        //            cellsone[i, 4].Value = dt.Rows[i - 4]["addressText"];
        //            cellsone[i, 5].Value = dt.Rows[i - 4]["totalMoney"];
        //            cellsone[i, 6].Value = dt.Rows[i - 4]["addTime"];
        //            cellsone[i, 7].Value = dt.Rows[i - 4]["payTime"];
        //            cellsone[i, 8].Value = dt.Rows[i - 4]["isPay"].ToString().Equals("1") ? "待支付" : "支付完成";
        //            cellsone[i, 9].Value = dt.Rows[i - 4]["isExpress"].ToString().Equals("3") ? "多次打单" : dt.Rows[i - 4]["express"].ToString().Equals("2") ? "已打单" : "未打单";
        //            cellsone[i, 10].Value = dt.Rows[i - 4]["isComDone"].ToString().Equals("1") ? "已结算" : "未结算";
        //        }
        //        wb.Save(Utils.GetMapPath(@read_uri + "/" + fileName), SaveFormat.Xlsx);
        //        Response.Redirect(ip_uri + "/excel/" + fileName);
        //    }
        //    else
        //    {
        //        JscriptMsg("无数据进行下载EXCEL！", Utils.CombUrlTxt("order_list.aspx", "start_time={0}&end_time={1}&status={2}&payWay={3}&express={4}&isDeal={5}&keywords={6}", txtStartTime.Text, txtEndTime.Text, this.ddlStatus.SelectedValue, this.ddlPayWay.SelectedValue, this.ddlExpress.SelectedValue, this.ddlDone.SelectedValue, txtKeyWords.Text));
        //    }
        //}

    }
}